package com.tensquare.article.controller;

import com.tensquare.article.pojo.Comment;
import com.tensquare.article.service.CommentService;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author: WangYaWei
 * @description:
 * @create: 2019-08-15 18:09
 **/
@RestController
@RequestMapping(value = "/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;

    /**
     * 新增评论
     * @param comment
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Comment comment){
        String userid = "1000";
        comment.setUserid(userid);
        comment.setPublishdata(new Date());
        commentService.add(comment);
        return new Result(true, StatusCode.OK, "评论成功");
    }

    /**
     * 文章id查询评论
     * @param articleid
     * @return
     */
    @GetMapping(value = "/article/{articleid}")
    public Result findByArticleid(@PathVariable String articleid){
        return new Result(true, StatusCode.OK, "查询成功", commentService.findByArticleid(articleid));
    }

    /**
     * 查询评论
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public Result deleteByid(@PathVariable String id){
        commentService.deleteById(id);
        return new Result(true, StatusCode.OK, "删除成功");
    }
}
